var dictionaryTyFormOptions = {
	type : 'post',
	beforeSubmit : dictionaryTyFormBeforeSubmit,
	success : dictionaryTyFormSuccess
};

function dictionaryTyFormBeforeSubmit(formData, jqForm, options) {
	return true;
}

function dictionaryTyFormSuccess(response, statusText) {
	if (statusText == 'success') {
		var message = null;

		if (response.message == undefined) {
			var $messageModal1 = $('#messageModal1');

			$messageModal1.find('.modal-title').html('提示').end().find('.modal-body').html("提交成功")
			$messageModal1.find('.modal-footer').show().end().find('.btn-default').hide();
			$messageModal1.find('.btn-primary').unbind().show().on('click', function() {
						parent.iframeModalClose();

						parent.searchDictionaryTy();

						$messageModal1.modal('hide');
					});

			$messageModal1.modal('show');

			return;
		}

		message = "提交失败:" + response.message;
	}

	if (message == null) {

		message = "提交失败"
	}

	var $messageModal1 = $('#messageModal1');

	$messageModal1.find('.modal-title').html('提示').end().find('.modal-body').html(message)
	$messageModal1.find('.modal-footer').show().end().find('.btn-default').hide();
	$messageModal1.find('.btn-primary').unbind().show().on('click', function() {
				$messageModal1.modal('hide');
			});

	$messageModal1.modal('show');
}

function initPage() {
	$(document).on('click', 'input[name^="dictionaryTy.configuration"]', function() {
				var checkedSize = $('input[name^="dictionaryTy.configuration"]:checked').size();

				if (checkedSize > 0) {
					$('.formBottomSubmit').show()
				} else {
					$('.formBottomSubmit').hide()
				}
			});

	$(document).on('click', 'button[name="closeDictionaryTy"],a[href="#toDictionaryTy"]', function() {
				parent.iframeModalClose();
			});

	$('form[name="dictionaryTyForm"]').validate({
				rules : {
					'dictionaryTy.code' : {
						required : true,
						maxlength : 36,
						'remote' : webContextUrl + '/admin/configuration/dictionaryTy/validationByCode.json?dictionaryTy.id=' + $('input[name="dictionaryTy.id"]').val()
					},
					'dictionaryTy.name' : {
						required : true,
						maxlength : 36
					},
					'dictionaryTy.orderNum' : {
						digits : true
					}
				},
				success : function(label, element) {
					var $element = $(element);

					$element.parent().removeClass('has-error').addClass('has-success');

					$element.siblings('.help-block').text($(label).text());
				},
				errorPlacement : function(label, element) {
					var $element = $(element);

					$element.parent().removeClass('has-success').addClass('has-error');

					$element.siblings('.help-block').text($(label).text());
				},
				submitHandler : function(form) {
					var $messageModal1 = $('#messageModal1');

					$messageModal1.find('.modal-title').html('提示');
					$messageModal1.find('.modal-body').html('正在处理中......')
					$messageModal1.find('.modal-footer').hide();

					$messageModal1.modal('show');

					$(form).ajaxSubmit(dictionaryTyFormOptions);
				},
				invalidHandler : function(form, validator) {
					var $messageModal1 = $('#messageModal1');

					$messageModal1.find('.modal-title').html('提示').end().find('.modal-body').html('表单验证不通过！')
					$messageModal1.find('.modal-footer').show().end().find('.btn-default').hide();
					$messageModal1.find('.btn-primary').unbind().show().on('click', function() {
								$messageModal1.modal('hide');
							});

					$messageModal1.modal('show');

					return false;
				}
			});
}

var useFiles = [];

useFiles.push('jquery-ui.font.css');
useFiles.push('page.css');
useFiles.push('jquery.validate');
useFiles.push('json2');
useFiles.push('bootstrap');
useFiles.push('jquery.form');
useFiles.push('jquery.validate.additional-methods');
useFiles.push('jquery.validate.messages_zh');
useFiles.push('fand');

seajs.use(useFiles, function() {
			$(initPage);
		});